home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / XMSLIB.ARJ / XMSTEST.C < prev   
C/C++ Source or Header  |  1991-06-07  |  6KB  |  248 lines

  1. /***************************************************************************
  2. *   XMSTEST.C                                                              *
  3. *   VERSION: 1.0                                                           *
  4. *   DATE:    06/07/91                                                      *
  5. *                                                                          *
  6. *   Copyright (c) 1991 James W. Birdsall. All Rights Reserved.             *
  7. *                                                                          *
  8. *   Compiles under Turbo C, Turbo C++, Borland C++.                        *
  9. *                                                                          *
  10. *   This is an example of the use of the XMSLIB functions. This simple     *
  11. *   demo program copies one file to another, using an EMB (extended memory *
  12. *   block) as a buffer. There must be a single EMB larger than the length  *
  13. *   of the file, since the entire file is stored at once.                  *            *
  14. *                                                                          *
  15. *   If the symbol LONG is defined, a large temporary buffer is used. If    *
  16. *   not, a small temporary buffer is used.                                 *
  17. *                                                                          *
  18. ***************************************************************************/
  19.  
  20. /*
  21. ** system includes <>
  22. */
  23.  
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <sys\stat.h>
  27.  
  28.  
  29. /*
  30. ** custom includes ""
  31. */
  32.  
  33. #include "xmslib.h"
  34.  
  35.  
  36. /*
  37. ** local #defines
  38. */
  39.  
  40. #ifdef LONG
  41. #define BUFFERSIZE 30000
  42. #else
  43. #define BUFFERSIZE 159
  44. #endif
  45.  
  46.  
  47. /*
  48. ** misc: copyright strings, version macros, etc.
  49. */
  50.  
  51. /*
  52. ** typedefs
  53. */
  54.  
  55. /*
  56. ** global variables
  57. */
  58.  
  59. unsigned char buffer[BUFFERSIZE];
  60.  
  61.  
  62. /*
  63. ** static globals
  64. */
  65.  
  66. /*
  67. ** function prototypes
  68. */
  69.  
  70. /*
  71. ** functions
  72. */
  73.  
  74.  
  75. main(int argc, char *argv[])
  76. {
  77.    FILE *infile, *outfile;
  78.    long filrem, MMoffset, freebytes;
  79.    struct stat statbuf;
  80.    int MMhandle;
  81.    unsigned int readsize;
  82.  
  83.    /*
  84.    ** Check for enough arguments.
  85.    */
  86.    if (argc < 3)
  87.    {
  88.       printf("Usage: %s infile outfile\n", argv[0]);
  89.       exit(3);
  90.    }
  91.  
  92.    /*
  93.    ** INITIALIZE XMSLIB! VERY IMPORTANT!
  94.    */
  95.    if (XMMlibinit() != 0)
  96.    {
  97.       printf("Error in XMSLIB initialization.\n");
  98.       exit(3);
  99.    }
  100.  
  101.    /*
  102.    ** Get size of largest EMB, total of all EMBs, display
  103.    */
  104.    freebytes = XMMallcoreleft();
  105.    if (_XMMerror != 0)
  106.    {
  107.        printf("Error %x in XMMallcoreleft()\n", _XMMerror);
  108.        exit(3);
  109.    }
  110.    printf("XMS: total free %lu bytes, ", freebytes);
  111.    freebytes = XMMcoreleft();
  112.    if (_XMMerror != 0)
  113.    {
  114.        printf("Error %x in XMMcoreleft()\n", _XMMerror);
  115.        exit(3);
  116.    }
  117.    printf("largest free EMB %lu bytes\n", freebytes);
  118.  
  119.    /*
  120.    ** Get size of input file so we know how many bytes to allocate.
  121.    */
  122.    if (stat(argv[1], &statbuf) != 0)
  123.    {
  124.       printf("stat() error.\n");
  125.       exit(3);
  126.    }
  127.    filrem = statbuf.st_size;
  128.    if (filrem > freebytes)
  129.    {
  130.       printf("Largest available EMB too small to copy file.\n");
  131.       exit(3);
  132.    }
  133.  
  134.    /*
  135.    ** Allocate EMB.
  136.    */
  137.    MMhandle = XMMalloc((unsigned long) filrem);
  138.    if (MMhandle == XMMOOPS)
  139.    {
  140.       printf("Error %x allocating EMB.\n", (int) _XMMerror);
  141.       exit(3);
  142.    }
  143.  
  144.    /*
  145.    ** Open input file.
  146.    */
  147.    if ((infile = fopen(argv[1], "rb")) == (FILE *) NULL)
  148.    {
  149.       printf("Error opening file %s\n", argv[1]);
  150.       XMMfree(MMhandle);
  151.       exit(3);
  152.    }
  153.  
  154.    /*
  155.    ** Main loop. Data is read from the input file into the temporary buffer,
  156.    ** then copied from the temporary buffer to the EMB.
  157.    */
  158.    MMoffset = 0;
  159.    printf("Reading file");
  160.    while (filrem > 0)
  161.    {
  162.       /* figure out how much to read */
  163.       readsize = ((filrem > BUFFERSIZE) ? BUFFERSIZE : (int)filrem);
  164.  
  165.       /* read */
  166.       if (fread(buffer, sizeof(char), readsize, infile) != readsize)
  167.       {
  168.          printf("Error reading file %s\n");
  169.          XMMfree(MMhandle);
  170.          exit(3);
  171.       }
  172.  
  173.       /* copy to XMS */
  174.       if (XMMcopyto((unsigned long) readsize, (unsigned char far *) buffer,
  175.                                 MMhandle, (unsigned long) MMoffset) == XMMOOPS)
  176.       {
  177.          printf("Error %x writing to EMB.\n", (int) _XMMerror);
  178.          XMMfree(MMhandle);
  179.          exit(3);
  180.       }
  181.  
  182.       /* display, update variables */
  183.       printf(".");
  184.       filrem -= readsize;
  185.       MMoffset += readsize;
  186.    }
  187.  
  188.    /*
  189.    ** Close input file, open output file.
  190.    */
  191.    fclose(infile);
  192.    if ((outfile = fopen(argv[2], "wb")) == (FILE *) NULL)
  193.    {
  194.       printf("Error opening file %s\n", argv[2]);
  195.       XMMfree(MMhandle);
  196.       exit(3);
  197.    }
  198.  
  199.    /*
  200.    ** The other main loop. Data is copied from XMS to the temporary buffer,
  201.    ** then written to the output file.
  202.    */
  203.    filrem = MMoffset;
  204.    MMoffset = 0;
  205.    printf("\nWriting file");
  206.    while (filrem > 0)
  207.    {
  208.       /* figure out how many bytes to copy */
  209.       readsize = ((filrem > BUFFERSIZE) ? BUFFERSIZE : (int)filrem);
  210.  
  211.       /* copy from XMS */
  212.       if (XMMcopyfrom((unsigned long) readsize, MMhandle,
  213.             (unsigned long) MMoffset, (unsigned char far *) buffer) == XMMOOPS)
  214.       {
  215.          printf("Error %x reading from EMB.\n", (int) _XMMerror);
  216.          XMMfree(MMhandle);
  217.          exit(3);
  218.       }
  219.  
  220.       /* write */
  221.       if (fwrite(buffer, sizeof(char), readsize, outfile) != readsize)
  222.       {
  223.          printf("Error writing file %s\n");
  224.          XMMfree(MMhandle);
  225.          exit(3);
  226.       }
  227.  
  228.       /* display, update variables */
  229.       printf(".");
  230.       filrem -= readsize;
  231.       MMoffset += readsize;
  232.    }
  233.  
  234.    /*
  235.    ** Close output file, free EMB.
  236.    */
  237.    fclose(outfile);
  238.    XMMfree(MMhandle);
  239.  
  240.    /*
  241.    ** Done.
  242.    */
  243.    printf("\nDone.\n");
  244.    exit(0);
  245. } /* end of main() */
  246.  
  247.  
  248.